﻿using UnityEngine;

namespace Core
{
    public class DSTimer : Component, IUpdate
    {
        public DSInt Data { get; private set; }
        public int Seconds { get; private set; }

        public float ElapsedTime { get; private set; }

        private bool running;

        public void Awake(DSInt t)
        {
            Data = t;
            running = false;
            ElapsedTime = 0;
            Data.Value = 0;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            Data = null;
        }

        public void Start()
        {
            running = true;
        }

        public void Pause()
        {
            running = false;
        }

        public void Reset()
        {
            ElapsedTime = 0;
            running = false;
            Seconds = 0;
            Data.Value = 0;
        }

        public void Update()
        {
            if (running == false) return;

            ElapsedTime += Time.deltaTime;
            int seconds = (int)ElapsedTime;
            if (Seconds != seconds)
            {
                Seconds = seconds;
                Data.Value = seconds;
            }
        }
    }
}
